package service

import (
	"context"
	"fmt"

	v1 "api/api/user/v1"
)

type UserService struct {
	v1.UnimplementedUserServiceServer
}

func NewUserService() *UserService {
	return &UserService{}
}

func (s *UserService) CreateUser(ctx context.Context, req *v1.CreateUserRequest) (*v1.CreateUserReply, error) {
	if err := req.Validate(); err != nil {
		return nil, err
	}
	user := v1.UserReplyWithoutPassword{
		Name:      req.GetName(),
		Telephone: req.GetTelephone(),
	}
	fmt.Printf("create user: %v\n", &user)
	return &v1.CreateUserReply{Result: &user}, nil
}

func (s *UserService) DeleteUser(ctx context.Context, req *v1.DeleteUserRequest) (*v1.DeleteUserReply, error) {
	if err := req.Validate(); err != nil {
		return nil, err
	}
	user := v1.UserReplyWithoutPassword{
		Id:        1,
		Name:      "tom",
		Telephone: "13262819719",
	}
	fmt.Printf("delete user id: %d\n", user.Id)
	return &v1.DeleteUserReply{Result: &user}, nil
}

func (s *UserService) GetUser(ctx context.Context, req *v1.GetUserRequest) (*v1.GetUserReply, error) {
	if err := req.Validate(); err != nil {
		return nil, err
	}
	user := v1.UserReplyWithoutPassword{
		Name:      "tom",
		Telephone: "13262819719",
	}
	fmt.Printf("get user: %v\n", &user)
	return &v1.GetUserReply{Result: &user}, nil
}

func (s *UserService) ListUser(ctx context.Context, req *v1.ListUserRequest) (*v1.ListUserReply, error) {
	if err := req.Validate(); err != nil {
		return nil, err
	}
	var users []*v1.UserReplyWithoutPassword
	user1 := v1.UserReplyWithoutPassword{
		Id:        1,
		Name:      "tom",
		Telephone: "13262819719",
	}
	user2 := v1.UserReplyWithoutPassword{
		Id:        2,
		Name:      "tom",
		Telephone: "13262819719",
	}
	users = append(users, &user1)
	users = append(users, &user2)
	fmt.Printf("list user: %v\n", users)
	return &v1.ListUserReply{Results: users}, nil
}

func (s *UserService) UpdateUser(ctx context.Context, req *v1.UpdateUserRequest) (*v1.UpdateUserReply, error) {
	if err := req.Validate(); err != nil {
		return nil, err
	}
	user := v1.UserReplyWithoutPassword{
		Id:        req.GetId(),
		Name:      req.GetName(),
		Telephone: req.GetTelephone(),
	}
	fmt.Printf("update user: %v\n", &user)
	return &v1.UpdateUserReply{Result: &user}, nil
}
